const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const currentDate=function() {
  // 创建一个 Date 对象
  var today = new Date();

  // 获取年、月、日
  var year = today.getFullYear();
  var month = today.getMonth() + 1; // 注意：月份从0开始，所以需要加1
  var day = today.getDate();
  return [year, month, day].map(formatNumber).join('-');
}

const currentDateTime = function(){
  var today = new Date();
  const year = today.getFullYear()
  const month = today.getMonth() + 1
  const day = today.getDate()
  const hour = today.getHours()
  const minute = today.getMinutes()
  const second = today.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatDate=function(date)
{
  const dateObj = new Date(date);

  // 获取年、月、日
  var year = dateObj.getFullYear();
  var month = dateObj.getMonth() + 1; // 注意：月份从0开始，所以需要加1
  var day = dateObj.getDate();
  return [year, month, day].map(formatNumber).join('-');
}

const daysDiff = function(dateStart,dateEnd) {
  // 将日期字符串转换为 Date 对象
  const d1 = new Date(dateStart);
  const d2 = new Date(dateEnd);

  // 计算时间差（单位：毫秒）
  const timeDiff = d2 - d1;

  // 将时间差转换为天数
  const daysDiff = timeDiff / (1000 * 60 * 60 * 24);

  return Math.abs(Math.round(daysDiff));
}

const addDays =function(date,daysDiff) {
  // 创建一个新的 Date 对象
  const dateObj = new Date(date);
  // 在当前日期基础上加一天
  dateObj.setDate(dateObj.getDate() + daysDiff);
  const year = dateObj.getFullYear()
  const month = dateObj.getMonth() + 1
  const day = dateObj.getDate()
  const hour = dateObj.getHours()
  const minute = dateObj.getMinutes()
  const second = dateObj.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const getDay = function(date) {
  const dateObj = new Date(date);
  const day = dateObj.getDate();
  return formatNumber(day);
}

const getWeekday = function(date) {
  const daysOfWeek = ["日", "一", "二", "三", "四", "五", "六"];
  const dateObj = new Date(date);
  const weekday = dateObj.getDay();
  return daysOfWeek[weekday];
}


module.exports = {
  currentDate: currentDate,
  currentDateTime: currentDateTime,
  daysDiff: daysDiff,
  getWeekday: getWeekday,
  addDays: addDays,
  getDay: getDay,
  formatNumber: formatNumber,
  formatDate: formatDate
}